package com.vvlive.caibao.smartcity.message;

import android.annotation.SuppressLint;
import android.os.Parcel;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * <b>好友请求通知</b> -- 张三(30029)请求加你为好友<br>
 * 						附加信息：快加我，快点。<br>
 * 						<b>params</b> senderName, senderId, extra, tag == 0;<br><br>
 * 
 * <b>好友拒绝通知</b> -- 李四(73830)拒绝了你的好友请求<br>
 * 						附加信息：我又不傻<br>
 *						<b>params:</b> senderName, senderId, extra, tag == -1;<br><br>
 *
 * <b>好友同意通知</b> -- 李四(73830)同意了你的好友请求<br>
 * 						<b>params:</b> senderName, senderId, extra, tag == 1;<br><br>
 * 
 * 
 * @author zhj
 *
 */
@SuppressLint("ParcelCreator")
@MessageTag(value = "RC:ContactNotifiMsg"/*, flag = MessageTag.ISPERSISTED | MessageTag.ISCOUNTED*/)
public class ContactNotification extends MessageContent {

	/** 好友请求 */
	public static final int REQUEST = 0;	
	/** 好友请求被拒绝 */
	public static final int REQEUST_DENIED = -1;	
	/** 好友请求已经被接受 */
	public static final int REQUEST_ACCEPTED = 1;	
	
	private String message;
	private int tag;

	/**
	 * 构造方法
	 * @param message	消息内容
	 * @param tag		标记 0-> 好友请求， -1->好友请求被拒绝， 1-> 好友请求被接受
	 */
	public ContactNotification(String message, int tag) {
		this.message = message;
		this.tag = tag;
	}

	public ContactNotification(byte[] data) {
		String jsonStr = null;
		try {
			jsonStr = new String(data, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		try {
			JSONObject jsonObj = new JSONObject(jsonStr);
			setMessage(jsonObj.getString("message"));
			setTag(jsonObj.getInt("tag"));

		} catch (JSONException e) {
			Log.e("JSONException", e.getMessage());
		}
	}

	@Override
	public byte[] encode() {
		JSONObject jsonObj = new JSONObject();
		try {
			jsonObj.put("message", message);
			jsonObj.put("tag", tag);

		} catch (JSONException e) {
			Log.e("JSONException", e.getMessage());
		}
		try {
			return jsonObj.toString().getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {

	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getTag() {
		return tag;
	}

	public void setTag(int tag) {
		this.tag = tag;
	}

}
